<?php
namespace Admin\Controller;
use Think\Controller;
use Admin\Common\AdminController;
class PicController extends AdminController{
    function showlist(){
        //接受传递过来的pics_id
        $pics_id = $_GET['pics_id'];
        $picture_info = D('Picture')
            ->where(array('pics_id'=>$pics_id))
            ->select();
        $this->assign('picture_info',$picture_info);
        $this->display();
    }
    function add(){
        $pics_id = $_GET['pics_id'];
        if(IS_POST){
            $z=$this->del_pics($pics_id);

            if($z){
                $this->success('相片添加成功',U('Pic/showlist',array('pics_id'=>$pics_id)),2);
            }else{
                $this->error('相片添加失败',U('Pic/add',array('pics_id'=>$pics_id)),3);
            }
        }
        $this->display();
    }
    function del_pics($id){
        $files = $_FILES['file'];

        foreach($files['error'] as $v) {
            if ($v == 0) {
                //设置保存的根路径
                $cfg = array(
                    'rootPath' => './Public/Uploads/picture/', //保存根路径
                );
                //实例化Uploads类
                $upd = new \Think\Upload($cfg);
                //调用批量上传的方法
                $z = $upd->upload(array($files));
//                dump($z);
                //给图片制作缩略图,遍历已经上传好的图片
                $im = new \Think\Image();
                foreach($z as $k=>$vv){
                    $old_path = $upd->rootPath.$vv['savepath'].$vv['savename'];
                    //制作logo
                    $im->open($old_path);
                    $im->thumb(110,73,6);
                    $pics_logo = $upd->rootPath.$vv['savepath'].'small_'.$vv['savename'];
                    $im->save($pics_logo);
                    //将缩略图存储给数据库
                    $data=array(
                        'pics_id'=>$id,
                        'picture_big'=>$old_path,
                        'picture_small'=>$pics_logo,
                    );
//                    dump($data);
                  D('Picture')->add($data);
                }
            return 1;
            }
        }
    }
}